import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import '../index.dart';

// 模糊弹框
class BlurAlert {

  // 弹出展示
  static void show(
      BuildContext context, {
        Key? key,
        required Widget child, // 展示的子部件
        bool dismissible = true, // 是否允许点击遮罩收起弹窗
        double blur = 5,//模糊程度,为0没有模糊效果
      }) {
    // 展示弹窗
    showDialog(
        context: context,
        barrierDismissible: dismissible,
        builder: (context) {
          return blurChild(context, child: child,dismissible: dismissible, blur: blur);
        });
  }

  /// 构建遮罩
  static Widget blurChild(BuildContext context,{
    Key? key,
    required Widget child, // 展示的子部件
    bool dismissible = true, // 是否允许点击遮罩收起弹窗
    double blur = 5,//模糊程度,为0没有模糊效果
  }){
    // 禁止侧滑或点击返回弹窗消失
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Stack(
        children: [
          /// 毛玻璃效果
          BackdropFilter(
            filter: ImageFilter.blur(
                sigmaX: blur, sigmaY: blur), //模糊程度
            child: InkWell(
              onTap: () {
                /// 隐藏弹框
                if (dismissible) Navigator.of(context).pop();
              },
              child: child,
            ),
          ),
        ],
      ),
    );
  }

  // 过度动画
  static void transitionAnimation(BuildContext context,{
    Key? key,
    bool dismissible = true, // 是否允许点击遮罩收起弹窗
    required Widget child, // 展示的子部件
  }) {
    // FadeRoute是自定义的切换过度动画（渐隐渐现） 如果不需要 可以使用默认的 MaterialPageRoute
    // Navigator.of(context).push(FadeRoute(page: view));
    Navigator.of(context).push(
      PageRouteBuilder(
        opaque: false,
        transitionDuration: const Duration(milliseconds: 500), // 设置动画的时长
        reverseTransitionDuration: const Duration(milliseconds: 500), // 设置关闭大图动画的时长
        pageBuilder: (context, animation, secondaryAnimation) {
          return FadeTransition(
              opacity: animation,
              child: Container(
                color: Colors.black.withOpacity(0.5),
                child: blurChild(context,child: child,dismissible: dismissible,blur: 5),
              ),
          );
        },
      ),
    );
  }

  // 提示信息
  static Widget promptContent(String text,{
    String? title,
    TextStyle? titleStyle,// 标题字体样式
    double? fontSize = 17, // 标题字体大小
    TextStyle? textStyle, // 文本内容字体样式
    double? textSize = 15, // 文本内容字体大小
    double? topSpace = 80, // 纵向外边距
    double? horizontal = 20, // 横向内边距
    double? vertical = 20, // 纵向内边距
    double? left = 20, // 按钮距边外边距
    String? sureText = '同意',
    String? cancelText = '拒绝',
    Function(String)?onClick,
  }) {
    return Container(
      width:double.infinity,
      margin: EdgeInsets.symmetric(horizontal: 30,vertical: topSpace??80.0),
      padding: EdgeInsets.symmetric(horizontal: horizontal??0,vertical: vertical??0),
      decoration: BoxDecoration(
        // 背景色
        color: AppColors.whiteBackground,
        borderRadius:  const BorderRadius.all(Radius.circular(15)),
      ),
      child: Column(
        children: [
          // 标题
          if (title != null)
            Text(title,
              style: titleStyle??TextStyle(
                color: AppColors.fixDark('#FFFFFF', '#2c2c2c'),
                fontSize: fontSize??17,
                fontWeight: FontWeight.bold,
              ),
            ),
          // 展示内容
          Expanded(
            child: Container(
              margin: const EdgeInsets.only(top: 10),
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: SingleChildScrollView(
                child: text.exStr.isHtml ? HtmlWidget(text.exStr) : Text(
                  text.exStr,
                  style: textStyle??TextStyle(
                    color: AppColors.fixDark('#FFFFFF', '#707070'),
                    fontSize: textSize??15,
                  ),
                ),
              ),
            ),
          ),

          // 确认按钮
          if (sureText != null)
            Container(
              width: double.infinity,
              height: 45,
              alignment: Alignment.center,
              margin: EdgeInsets.only(top: 10, left: left??0,right: left??0),
              decoration: const BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(10)),
                //渐变色
                gradient: LinearGradient(
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                    colors: [
                      Colors.orange,
                      Colors.blue,
                    ]),
              ),
              child: Text(
                sureText,
                style: TextStyle(
                  color: AppColors.fixDark('#FFFFFF', '#FFFFFF'),
                  fontSize: 17,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ).withOnTap((){
              // 点击同意
              if (onClick != null) onClick(sureText);
            }),
          // 取消按钮
          if (cancelText != null)
            Container(
              width: double.infinity,
              height: 40,
              alignment: Alignment.center,
              margin: EdgeInsets.only(top: 10, left: left??0,right: left??0),
              child:Text(
                cancelText,
                style: TextStyle(
                  color: AppColors.fixDark('#FFFFFF', '707070'),
                  fontSize: 17,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ).withOnTap((){
              // 点击拒绝
              if (onClick != null) onClick(cancelText);
            }),
        ],
      ),
    );
  }

  /// 覆盖弹框
  static showOverlayDialog(BuildContext context,{Map<String, dynamic>? data, String? key}) {
    if (data == null) return;
    OverlayState overlayState = Overlay.of(context);
    OverlayEntry? overlayEntry;
    overlayEntry = OverlayEntry(
      builder: (BuildContext context) {
        return promptContent(
          data['content'],
          title: data['title']??'公告',
          fontSize: 20,
          textSize: 18,
          topSpace: 140,
          vertical: 30,
          left: 35,
          sureText: '我知道了',
          cancelText: null,
          onClick: (value) {
            if (value == '我知道了') {
              if (key != null ) Storage().setString(key, "1"); // 记录已弹出(只弹一次)
              overlayEntry!.remove(); // 关闭弹框
            }
          },
        ).withBlur(context, 7);
      },
    );
    // 展示覆盖弹框
    overlayState.insert(overlayEntry);
  }

}